<template>
  <div>
    <el-dialog
      :title="info.isAdd ? '分类添加' : '分类编辑'"
      :visible.sync="info.isshow" @closed="close"
    >
      <el-form :model="form">
        {{ form }}
        <el-form-item label="上级分类" label-width="120px">
          <el-select v-model="form.pid">
            <el-option label="请选择" value="" disabled></el-option>
            <el-option label="顶级分类" :value="0"></el-option>
            <el-option
              v-for="item in cateList"
              :key="item.id"
              :label="item.catename"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="分类名称" label-width="120px">
          <el-input v-model="form.catename" autocomplete="off"></el-input>
        </el-form-item>
        <!-- 图片 -->
        <el-form-item label="图片" label-width="120px">
          <el-upload
            class="avatar-uploader"
            action="#"
            :show-file-list="false"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imgUrl" :src="imgUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <!-- 状态 -->
        <el-form-item label="状态" label-width="120px">
          <el-switch
            v-model="form.status"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="quxiao">取 消</el-button>
        <el-button type="primary" @click="add" v-if="info.isAdd"
          >添 加</el-button
        >
        <el-button type="primary" v-else @click="xiugai">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import { reqcateadd, reqcateedit, reqcateinfo } from "../../../request/api";
import { errorAlert, successAlert } from "../../../utils/alert";
import path from "path";
export default {
  props: ["info"],
  data() {
    return {
      imgUrl: "",
      form: {
        pid: "",
        catename: "",
        img: null,
        status: 1,
      },
    };
  },
  computed: {
    ...mapGetters({
      //分类list
      cateList: "fenlei/list",
    }),
  },
  mounted() {},
  methods: {
    ...mapActions({
      //获取分类list
      reqList: "fenlei/reqList",
    }),

    //获取文件
    getFile(e) {
      let file = e.target.files[0];
      if (file.size > 2 * 1024 * 1024) {
        errorAlert("文件最大2M");
        return;
      }
      // 判断文件类型
      let imgArr = [".png", ".jpg", ".jpeg", ".gif"];
      let extname = path.extname(file.name);

      if (!imgArr.find((item) => item == extname)) {
        errorAlert("请上传正确格式的文件");
        return;
      }
      this.imgUrl = URL.createObjectURL(file);
      this.form.img = file;
    },
    //element-ui上传之前
    beforeAvatarUpload(file) {
      //判断大小 
      if (file.size > 2 * 1024 * 1024) {
        errorAlert("文件最大2M");
        return;
      }

      // 判断文件类型
      let imgArr = [".png", ".jpg", ".jpeg", ".gif"];
      let extname = path.extname(file.name);
      if (!imgArr.find((item) => item == extname)) {
        errorAlert("请上传正确格式的文件");
        return;
      }
      this.imgUrl = URL.createObjectURL(file);
      this.form.img = file;
    },
    //取消
    quxiao() {
      this.info.isshow = false;
    },
    //清空
    qingkong() {
      this.form = {
        pid: "",
        catename: "",
        status: 1,
        img: null,
      };
      this.imgUrl = "";
    },
    //添加
    add() {
      reqcateadd(this.form).then((res) => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          this.qingkong();
          this.info.isshow = false;
          this.reqList();
        }
      });
    },
    //获取一条数据
    getOne(id) {
      reqcateinfo({ id: id }).then((res) => {
        if (res.data.code == 200) {
          this.form = res.data.list; 
          this.form.id = id;
          this.imgUrl = this.$pre + this.form.img;
        }
      });
    },
    //修改
    xiugai() {
      reqcateedit(this.form).then((res) => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          this.qingkong();
          this.info.isshow = false;
          this.reqList();
        }
      });
    },
    //弹框关闭
    close() {
      !this.info.isAdd && this.qingkong();
    },
  },
};
</script>
<style scoped lang="less">
@import "../../../less/index.less";
/deep/ .avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>